package com.dayatang.hrm.compensation.domain;

import java.util.Date;

import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.Transient;

import com.dayatang.commons.domain.InstanceFactory;
import com.dayatang.commons.domain.Value;
import com.dayatang.hrm.staffing.domain.Employee;

/**
 * 固定员工薪资参数
 * 
 * @author chencao
 * 
 */
@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorValue("3")
public class FixedEmployeeSalaryParameter extends SalaryParameter {

	/**
	 * 
	 */
	private static final long serialVersionUID = 8851126278238267975L;

	private static FixedEmployeeSalaryParameterRepository fixedEmployeeSalaryParameterRepository;

	/*
	 * =======================================
	 * 
	 * transient
	 * 
	 * ========================================
	 */
	/**
	 * latestSetting为薪资参数具体设置的一个代理
	 */
	@Transient
	private FixedEmployeeSalaryParameterSetting latestSetting;

	public FixedEmployeeSalaryParameter() {
		if (latestSetting == null) {
			latestSetting = FixedEmployeeSalaryParameterSetting.init(this);
		}
	}

	public static FixedEmployeeSalaryParameterRepository getFixedEmployeeSalaryParameterRepository() {
		if (fixedEmployeeSalaryParameterRepository == null) {
			fixedEmployeeSalaryParameterRepository = InstanceFactory
					.getInstance(FixedEmployeeSalaryParameterRepository.class);
		}
		return fixedEmployeeSalaryParameterRepository;
	}

	public static void setFixedEmployeeSalaryParameterRepository(
			FixedEmployeeSalaryParameterRepository fixedEmployeeSalaryParameterRepository) {
		FixedEmployeeSalaryParameter.fixedEmployeeSalaryParameterRepository = fixedEmployeeSalaryParameterRepository;
	}

	FixedEmployeeSalaryParameterSetting getLatestSetting() {
		return latestSetting;
	}

	void setLatestSetting(FixedEmployeeSalaryParameterSetting latestSetting) {
		this.latestSetting = latestSetting;
	}

	// 代理属性

	/*
	 * =======================================
	 * 
	 * action
	 * 
	 * ========================================
	 */

	@Override
	protected Value calculateValue(Employee employee, Date fromDate, Date toDate) {
		return getFixedEmployeeSalaryParameterRepository().calculateValue(this,
				employee, fromDate, toDate);
	}

	public static FixedEmployeeSalaryParameter getLatest(Long id) {
		return get(id, new Date());
	}

	public static FixedEmployeeSalaryParameter get(Long id, Date queryDate) {
		FixedEmployeeSalaryParameter parameter = getSalaryParameterRepository()
				.get(FixedEmployeeSalaryParameter.class, id);
		if (parameter != null) {
			FixedEmployeeSalaryParameterSetting setting = FixedEmployeeSalaryParameterSetting
					.get(parameter, queryDate);
			if (setting == null) {
				setting = FixedEmployeeSalaryParameterSetting.init(parameter);
			}
			parameter.setLatestSetting(setting);
		}
		return parameter;
	}

}
